
//
//  SQMessageViewController.m
//  SQ_NEW
//
//  Created by 刘明 on 2017/5/10.
//  Copyright © 2017年 刘明. All rights reserved.
//

#import "SQMessageViewController.h"
#import "SQSocket.h"
#import "SQMessageTableViewCell.h"
#import "AppDelegate.h"

@interface SQMessageViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@end

@implementation SQMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupNav];
    
    [self setupTableView];
    
    [self setupRefresh];

    
   AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    self.dataArray = app.dataArray;
}

#pragma mark - requestNewData

- (void)requestNewData{
    
    [self.tableView.mj_header endRefreshing];
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    self.dataArray = app.dataArray;
    
    [self.tableView reloadData];
}

- (void)setupRefresh{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestNewData)];
    
    self.tableView.mj_header = header;
    
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"加载中,请稍后" forState:MJRefreshStateRefreshing];
    header.stateLabel.font = [UIFont systemFontOfSize:15];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    [header beginRefreshing];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
}

- (void)setupTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
    [self.tableView registerClass:[SQMessageTableViewCell class] forCellReuseIdentifier:@"SQMessageTableViewCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    

}
#pragma mark - emptyDelegate

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    // Do something
    [self requestNewData];
}

#pragma mark - LMTableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SQMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SQMessageTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[SQMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SQMessageTableViewCell"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.socketModel = self.dataArray[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



#pragma mark - 导航栏

- (void)setupNav{
    
    
}


- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _dataArray;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
